import {MutationTree, ActionTree} from 'vuex'
import {serBaseUrl, serFileUrl, ciipSerUrl, operationWebUrl} from '~/../config/server.url.conf'

interface defState {
    token: string
    haslogin: boolean
    userinfo: any
    userNum: number
    baseurl: string
    operationUrl: string
    basefile: string
    ciipWebUrl: string
    shopInfo: any
    tree:any
    treeNum: number
    classid:any
    classidNum: number,
    institute: any
}

export const actions: ActionTree<defState, defState> = {
    // async nuxtServerInit ({commit}, context) {
    //     //
    // }
}
export const state = (): defState => ({
    token: '',
    haslogin: false,
    userinfo: {},
    userNum: 0, // 获取用户信息次数  大于0表示已经验证用户是否登录
    baseurl: serBaseUrl,
    operationUrl: operationWebUrl,
    basefile: serFileUrl,
    ciipWebUrl: ciipSerUrl,
    shopInfo: {
        data: {}, // 店铺信息 公共店铺头部信息维护
        id: '' // 店铺id 可通过监听id变化自动更新店铺信息
    },
    tree: [],
    treeNum: 0,
    classid: [],
    classidNum: 0,
    institute: {}
})

export const mutations: MutationTree<defState> = {
    setUserInfo (state: defState, json: any) {
        state.userinfo = json
        state.haslogin = !!(json && json.id)
        state.token = json.uuid || ''
    },
    setUserNum (state: defState) {
        state.userNum++
    },
    setShopInfo (state: defState, json: any) {
        state.shopInfo.data = json || {}
    },
    setShopId (state: defState, id: string | number) {
        state.shopInfo.id = id
    },
    setTree (state: defState, json: any) {
        state.tree = json
        state.treeNum++
    },
    setClassid (state: defState, json: any) {
        state.classid = json
        state.classidNum++
    },
    setShopBanner (state: defState, json: any) {
        if (state.shopInfo.data.id) {
            state.shopInfo.data.header.banner = json
        }
    },
    setInstitute (state: defState, json: any) {
        state.institute = json || {}
    }
}
